package com.student.education.service.impl;

import com.github.pagehelper.PageHelper;
import com.student.education.common.CommonPage;
import com.student.education.entity.Consumption;
import com.student.education.dao.ConsumptionDao;
import com.student.education.entity.ConsumptionCountOne;
import com.student.education.entity.ConsumptionCountTwo;
import com.student.education.service.ConsumptionService;
import com.student.education.service.UserService;
import com.student.education.to.ConsumptionCountTo;
import com.student.education.to.ConsumptionQueryTo;
import com.student.education.to.ConsumptionTo;
import com.student.education.to.UserTo;
import com.student.education.vo.ConsumptionVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * 消费信息表(Consumption)表服务实现类
 *
 * @author makejava
 * @since 2021-10-06 21:56:55
 */
@Service("consumptionService")
public class ConsumptionServiceImpl implements ConsumptionService {
    @Resource
    private ConsumptionDao consumptionDao;
    @Resource
    private UserService userService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Consumption queryById(Integer id) {
        return this.consumptionDao.queryById(id);
    }


    /**
     * 新增数据
     *
     * @param vo 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(List<ConsumptionTo> vo) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
        try {
            for (int i=0;i<vo.size();i++){
                String month = vo.get(i).getDate();
                Date dateStart = dateFormat.parse(month);
                vo.get(i).setDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(dateStart));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return this.consumptionDao.insert(vo);
    }

    /**
     * 修改数据
     *
     * @param consumption 实例对象
     * @return 实例对象
     */
    @Override
    public Consumption update(Consumption consumption) {
        this.consumptionDao.update(consumption);
        return this.queryById(consumption.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.consumptionDao.deleteById(id) > 0;
    }

    @Override
    public CommonPage<ConsumptionQueryTo> query(ConsumptionVo vo) {

        UserTo userTo = userService.queryById(vo.getUserId());
        List<ConsumptionQueryTo> list = new ArrayList<>();
        //学生
        if ("3".equals(userTo.getRoleId())){
            if (vo.getPageUtil() != null) {
                PageHelper.startPage(vo.getPageUtil().getPageNumber(), vo.getPageUtil().getPageSize());
            }
            list=consumptionDao.queryByStudent(vo);
        }else {
            if (vo.getPageUtil() != null) {
                PageHelper.startPage(vo.getPageUtil().getPageNumber(), vo.getPageUtil().getPageSize());
            }
            list=consumptionDao.queryAll(vo);
        }
        return CommonPage.restPage(list);
    }

    @Override
    public ConsumptionCountTo count(Integer useId) {
        UserTo userTo = userService.queryById(useId);
        ConsumptionCountTo list = new ConsumptionCountTo();
        //学生
        if ("3".equals(userTo.getRoleId())){
            List<ConsumptionCountOne> type=consumptionDao.consumptionCountOne(useId);
            list.setType(type);
            List<ConsumptionCountTwo> month = consumptionDao.consumptionCountTwo(useId);
            list.setMonth(month);
            list.setCount(consumptionDao.consumptionCountThree(useId));
        }else {
            if (userTo.getClassId()==null){
                return list;
            }
            List<ConsumptionCountOne> type=consumptionDao.consumptionCountOneAll(userTo.getClassId());
            list.setType(type);
            List<ConsumptionCountTwo> month = consumptionDao.consumptionCountTwoAll(userTo.getClassId());
            list.setMonth(month);
            list.setCount(consumptionDao.consumptionCountThreeAll(userTo.getClassId()));
        }

        return list;
    }
}
